package com.sanpu.kxe.module.stepview;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sanpu.kxe.R;
import com.sanpu.kxe.module.stepview.bean.StepBean;

import java.util.List;

/**
 * Description：水平排列的stepview
 * Created by 薛勇军 on 2018/7/9.
 */

public class HorizontalStepView extends LinearLayout implements HorizontalStepsViewIndicator.OnDrawIndicatorListener {
    private RelativeLayout mTextContainer;
    private RelativeLayout mArrowContainer;
    private HorizontalStepsViewIndicator mStepsViewIndicator;
    private List<StepBean> mStepBeanList;
    private int mComplectingPosition;
    private int mUnComplectedTextColor = ContextCompat.getColor(getContext(), R.color.c_909090);//定义默认未完成文字的颜色;
    private int mComplectedTextColor = ContextCompat.getColor(getContext(), R.color.c_ffcc39);//定义默认完成文字的颜色;
    private int mTextSize = 10;//default textSize
    private int mArrowPosition = 0;//default
    private TextView mTextView;
    private ImageView mImageView;

    public HorizontalStepView(Context context) {
        this(context, null);
    }

    public HorizontalStepView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HorizontalStepView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        View rootView = LayoutInflater.from(getContext()).inflate(R.layout.widget_horizontal_stepsview, this);
        mStepsViewIndicator = (HorizontalStepsViewIndicator) rootView.findViewById(R.id.steps_indicator);
        mStepsViewIndicator.setOnDrawListener(this);

        mTextContainer = (RelativeLayout) rootView.findViewById(R.id.rl_text_container);
        mArrowContainer = (RelativeLayout) rootView.findViewById(R.id.rl_arrow_container);
    }

    public void showArrowContainer(boolean isShow) {
        mArrowContainer.setVisibility(isShow ? VISIBLE : GONE);
    }

    public void setDefaultStepIndicatorNum(int defaultStepIndicatorNum) {
        mStepsViewIndicator.setDefaultStepIndicatorNum(defaultStepIndicatorNum);
    }

    public void setOnClickSetupListener(HorizontalStepsViewIndicator.OnClickSetupViewListener mOnClickSetupListener) {
        mStepsViewIndicator.setOnClickSetupListener(mOnClickSetupListener);
    }

    /**
     * 设置显示的文字
     *
     * @param stepsBeanList
     * @return
     */
    public HorizontalStepView setStepViewTexts(List<StepBean> stepsBeanList, float linePadding) {
        mStepBeanList = stepsBeanList;
        mStepsViewIndicator.setStepNum(mStepBeanList, linePadding);
        return this;
    }

    /**
     * 设置未完成文字的颜色
     *
     * @param unComplectedTextColor
     * @return
     */
    public HorizontalStepView setStepViewUnComplectedTextColor(int unComplectedTextColor) {
        mUnComplectedTextColor = unComplectedTextColor;
        return this;
    }

    /**
     * 设置完成文字的颜色
     *
     * @param complectedTextColor
     * @return
     */
    public HorizontalStepView setStepViewComplectedTextColor(int complectedTextColor) {
        this.mComplectedTextColor = complectedTextColor;
        return this;
    }

    /**
     * 设置StepsViewIndicator未完成线的颜色
     *
     * @param unCompletedLineColor
     * @return
     */
    public HorizontalStepView setStepsViewIndicatorUnCompletedLineColor(int unCompletedLineColor) {
        mStepsViewIndicator.setUnCompletedLineColor(unCompletedLineColor);
        return this;
    }

    /**
     * 设置StepsViewIndicator完成线的颜色
     *
     * @param completedLineColor
     * @return
     */
    public HorizontalStepView setStepsViewIndicatorCompletedLineColor(int completedLineColor) {
        mStepsViewIndicator.setCompletedLineColor(completedLineColor);
        return this;
    }

    /**
     * 设置StepsViewIndicator默认图片
     *
     * @param defaultIcon
     */
    public HorizontalStepView setStepsViewIndicatorDefaultIcon(Drawable defaultIcon) {
        mStepsViewIndicator.setDefaultIcon(defaultIcon);
        return this;
    }

    /**
     * 设置StepsViewIndicator已完成图片
     *
     * @param completeIcon
     */
    public HorizontalStepView setStepsViewIndicatorCompleteIcon(Drawable completeIcon) {
        mStepsViewIndicator.setCompleteIcon(completeIcon);
        return this;
    }

    /**
     * 设置StepsViewIndicator正在进行中的图片
     *
     * @param attentionIcon
     */
    public HorizontalStepView setStepsViewIndicatorAttentionIcon(Drawable attentionIcon) {
        mStepsViewIndicator.setAttentionIcon(attentionIcon);
        return this;
    }

    /**
     * 设置StepsViewIndicator正在进行中的图片
     */
    public HorizontalStepView setStepsViewIndicatorDefaultLine(Drawable defaultLine) {
        mStepsViewIndicator.setDefaultLine(defaultLine);
        return this;
    }

    public HorizontalStepView setCompletedLineHeight(float completedLineHeight) {
        mStepsViewIndicator.setCompletedLineHeight(completedLineHeight);
        return this;
    }

    /**
     * set textSize
     *
     * @param textSize
     * @return
     */
    public HorizontalStepView setTextSize(int textSize) {
        if (textSize > 0) {
            mTextSize = textSize;
        }
        return this;
    }

    public void setArrowPostion(int mArrowPosition) {
        this.mArrowPosition = mArrowPosition;
//        ondrawIndicator(mArrowPosition);
    }

    public void setCheckPosition(int mCheckPosition) {
        mStepsViewIndicator.setCheckPosition(mCheckPosition);
        invalidate();
    }

    @Override
    public void ondrawIndicator(int position) {
        if (mTextContainer != null) {
            mTextContainer.removeAllViews();
            mArrowContainer.removeAllViews();
//            mArrowPosition = mArrowPosition == 0 ? position : mArrowPosition;
            List<Float> complectedXPosition = mStepsViewIndicator.getCircleCenterPointPositionList();
            if (mStepBeanList != null && complectedXPosition != null && complectedXPosition.size() > 0) {
                for (int i = 0; i < mStepBeanList.size(); i++) {
                    mTextView = new TextView(getContext());
                    mTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, mTextSize);
                    mTextView.setText(mStepBeanList.get(i).getName());
                    int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
                    mTextView.measure(spec, spec);
                    // getMeasuredWidth
                    int measuredWidth = mTextView.getMeasuredWidth();
                    mTextView.setX(complectedXPosition.get(i) - measuredWidth / 2);
                    mTextView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

//                    if(i <= mComplectingPosition)
//                    {
//                        mTextView.setTypeface(null, Typeface.BOLD);
//                        mTextView.setTextColor(mComplectedTextColor);
//                    } else
//                    {
//                        mTextView.setTextColor(mUnComplectedTextColor);
//                    }
                    if (position == i) {
                        mTextView.setTextColor(mComplectedTextColor);
                    } else {
                        mTextView.setTextColor(mUnComplectedTextColor);
                    }

                    if (mArrowPosition == i) {
                        mImageView = new ImageView(getContext());
                        mImageView.setImageResource(R.drawable.ic_setup_arrow);
                        mImageView.setX(complectedXPosition.get(i) - measuredWidth / 4);
                        mArrowContainer.addView(mImageView);
                    }
                    mTextContainer.addView(mTextView);
                }
            }
        }
    }
}
